package com.crm.common.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {
	private static final String KEY = "maoteng";   // 密钥

	// 接收数据，生成token并返回
	public static String getToken(Map<String, Object> claims) {
		return JWT.create()
				.withClaim("claims", claims)
				.withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60))       // 失效时间1小时
				.sign(Algorithm.HMAC256(KEY));
	}

	// 接收token,验证并返回数据
	public static Map<String, Object> parseToken(String token) {
		return JWT.require(Algorithm.HMAC256(KEY))
				.build()
				.verify(token)
				.getClaim("claims")
				.asMap();
	}
}

